home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / iritsm3s.zip / CAGDCMPT.C < prev    next >
C/C++ Source or Header  |  1991-10-09  |  4KB  |  127 lines

  1. /******************************************************************************
  2. * CagdCmpt.c - Make objects compatible.                          *
  3. *******************************************************************************
  4. * Written by Gershon Elber, Sep. 91.                          *
  5. ******************************************************************************/
  6.  
  7. #ifdef __MSDOS__
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <alloc.h>
  11. #endif /* __MSDOS__ */
  12.  
  13. #include "cagd_loc.h"
  14.  
  15. /******************************************************************************
  16. * Given two curves, make the compatible by:                      *
  17. * 1. Coercing their point type to be the same.                      *
  18. * 2. Make them have the same curve type.                      *
  19. * 3. Raising the degree of the lower one to be the same.              *
  20. * 4. Refine them to a common knot vector (If Bspline).                  *
  21. * Note 3 is preformed if SameOrder TRUE, 4 if SameKV TRUE.              *
  22. * Returns TRUE if succesfull. Both Curves are modified IN PLACE.          *
  23. ******************************************************************************/
  24. CagdBType CagdMakeCrvsCompatible(CagdCrvStruct **Crv1, CagdCrvStruct **Crv2,
  25.                  CagdBType SameOrder, CagdBType SameKV)
  26. {
  27.     int i, KV1Len, KV2Len, RefLen;
  28.     CagdRType *KV1, *KV2, *RefKV;
  29.     CagdCrvStruct *TCrv;
  30.     CagdPointType
  31.     CommonPType = CagdMergePointType((*Crv1) -> PType, (*Crv2) -> PType);
  32.  
  33.     /* Make the point types compatible. */
  34.     if (CommonPType != (*Crv1) -> PType) {
  35.     TCrv = CagdCoerceCrvTo(*Crv1, CommonPType);
  36.     CagdCrvFree(*Crv1);
  37.     *Crv1 = TCrv;
  38.     }
  39.     if (CommonPType != (*Crv2) -> PType) {
  40.     TCrv = CagdCoerceCrvTo(*Crv2, CommonPType);
  41.     CagdCrvFree(*Crv2);
  42.     *Crv2 = TCrv;
  43.     }
  44.  
  45.     /* If incompatible curve type - make it the same as well. */
  46.     if ((*Crv1) -> GType != (*Crv2) -> GType) {
  47.     /* If power basis - promote to bezier: */
  48.     if ((*Crv1) -> GType == CAGD_CPOWER_TYPE) {
  49.         TCrv = CnvrtPower2BezierCrv(*Crv1);
  50.         CagdCrvFree(*Crv1);
  51.         *Crv1 = TCrv;
  52.     }
  53.     if ((*Crv2) -> GType == CAGD_CPOWER_TYPE) {
  54.         TCrv = CnvrtPower2BezierCrv(*Crv2);
  55.         CagdCrvFree(*Crv2);
  56.         *Crv2 = TCrv;
  57.     }
  58.  
  59.     /* Now both curves may be either bezier or bspline curves. */
  60.     if ((*Crv1) -> GType != (*Crv2) -> GType) {
  61.         /* If bezier basis - promote to bspline: */
  62.         if ((*Crv1) -> GType == CAGD_CBEZIER_TYPE) {
  63.         TCrv = CnvrtBezier2BsplineCrv(*Crv1);
  64.         CagdCrvFree(*Crv1);
  65.         *Crv1 = TCrv;
  66.         }
  67.         if ((*Crv2) -> GType == CAGD_CBEZIER_TYPE) {
  68.         TCrv = CnvrtBezier2BsplineCrv(*Crv2);
  69.         CagdCrvFree(*Crv2);
  70.         *Crv2 = TCrv;
  71.         }
  72.  
  73.     }
  74.     }
  75.  
  76.     if (SameOrder) {
  77.     /* Raise the degree of the lower one. */
  78.     for (i = (*Crv1) -> Order; i < (*Crv2) -> Order; i++) {
  79.         TCrv = CagdCrvDegreeRaise(*Crv1);
  80.         CagdCrvFree(*Crv1);
  81.         *Crv1 = TCrv;
  82.     }
  83.     for (i = (*Crv2) -> Order; i < (*Crv1) -> Order; i++) {
  84.         TCrv = CagdCrvDegreeRaise(*Crv2);
  85.         CagdCrvFree(*Crv2);
  86.         *Crv2 = TCrv;
  87.     }
  88.     }
  89.  
  90.     if (SameKV) {
  91.     /* If bspline curve - make sure knot vectors are the same. */
  92.     if ((*Crv1) -> GType == CAGD_CBSPLINE_TYPE) {
  93.         KV1 = (*Crv1) -> KnotVector;
  94.         KV2 = (*Crv2) -> KnotVector;
  95.         KV1Len = (*Crv1) -> Length + (*Crv1) -> Order;
  96.         KV2Len = (*Crv2) -> Length + (*Crv2) -> Order;
  97.  
  98.         /* Affine map second knot vector to span same parametric domain. */
  99.         BspKnotAffineTrans(KV2, KV2Len, KV1[0] - KV2[0],
  100.                    (KV1[KV1Len - 1] - KV1[0]) /
  101.                    (KV2[KV2Len - 1] - KV2[0]));
  102.  
  103.         /* Find knots in KV2 which are not in KV1 and refine Crv1 there. */
  104.         RefKV  = BspKnotSubtrTwo(KV2, KV2Len, KV1, KV1Len, &RefLen);
  105.         if (RefLen > 0) {
  106.         TCrv = CagdCrvRefineAtParams(*Crv1, FALSE, RefKV, RefLen);
  107.         CagdCrvFree(*Crv1);
  108.         *Crv1 = TCrv;
  109.         KV1 = (*Crv1) -> KnotVector;
  110.         KV1Len = (*Crv1) -> Length + (*Crv1) -> Order;
  111.         }
  112.         CagdFree((VoidPtr) RefKV);
  113.  
  114.         /* Find knots in KV1 which are not in KV2 and refine Crv2 there. */
  115.         RefKV  = BspKnotSubtrTwo(KV1, KV1Len, KV2, KV2Len, &RefLen);
  116.         if (RefLen > 0) {
  117.         TCrv = CagdCrvRefineAtParams(*Crv2, FALSE, RefKV, RefLen);
  118.         CagdCrvFree(*Crv2);
  119.         *Crv2 = TCrv;
  120.         }
  121.         CagdFree((VoidPtr) RefKV);
  122.     }
  123.     }
  124.  
  125.     return TRUE;
  126. }
  127.